package com.kantboot.wechat.applet.service.impl;

import com.kantboot.wechat.applet.config.WechatAppletConfig;
import com.kantboot.wechat.applet.entity.PhoneNumberInfo;
import com.kantboot.wechat.applet.entity.SessionKeyInfo;
import com.kantboot.wechat.applet.entity.UserInfo;
import com.kantboot.wechat.applet.service.InfoService;
import com.kantboot.wechat.applet.entity.Code2SessionKeyInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class InfoServiceImpl implements InfoService {

    @Resource
    WechatAppletConfig wechatAppletConfig;

    @Override
    public SessionKeyInfo createSessionKeyInfo(String code) {
        return new Code2SessionKeyInfo()
                .setAppid(wechatAppletConfig.getAppid())
                .setSecret(wechatAppletConfig.getSecret())
                .setCode(code)
                .createSessionKeyInfo();
    }

    @Override
    public PhoneNumberInfo createPhoneNumberInfo(
            SessionKeyInfo sessionKeyInfo,
            String encryptedData,
            String iv) {
        return sessionKeyInfo.createPhoneNumberInfo(encryptedData,iv);
    }

    @Override
    public PhoneNumberInfo createPhoneNumberInfo(String code, String encryptedData, String iv) {
        SessionKeyInfo sessionKeyInfo = new Code2SessionKeyInfo()
                .setAppid(wechatAppletConfig.getAppid())
                .setSecret(wechatAppletConfig.getSecret())
                .setCode(code)
                .createSessionKeyInfo();
        return sessionKeyInfo.createPhoneNumberInfo(encryptedData,iv);
    }

    @Override
    public UserInfo createUserInfo(SessionKeyInfo sessionKeyInfo, String encryptedData, String iv) {
        return sessionKeyInfo.createUserInfo(encryptedData,iv);
    }

    @Override
    public UserInfo createUserInfo(String code, String encryptedData, String iv) {
        SessionKeyInfo sessionKeyInfo = new Code2SessionKeyInfo()
                .setAppid(wechatAppletConfig.getAppid())
                .setSecret(wechatAppletConfig.getSecret())
                .setCode(code)
                .createSessionKeyInfo();
        return sessionKeyInfo.createUserInfo(encryptedData,iv);
    }

}
